/*
 @Date: 2022/5/3
 @Desc: 数据处理 协议
 */

import UIKit

/// 区块数据结构
struct IPHomeSectionData {
    var headerReuseId: String!
    var headerSize: CGSize = .zero
    
    var cellReuseId: String!
    var itemSize: CGSize = .zero
    var items: [IPHomeItem]?
}

struct IPHomeItem {
    var iconName: String?
    var title: String?
}

/// 数据处理类
class IPHomeDataHandler {

    /// 数据保存
    private lazy var mDatasource: [IPHomeSectionData] = []
    /// 刷新
    var reloadDataBlock:(()->Void)?
    
    /// 开始数据配置
    func setupData(_ width: CGFloat) {
        
        let f = configureBannerData(width)
        mDatasource.append(f)
        
        let s = configureItemData(width)
        mDatasource.append(s)
        
        reloadDataBlock?()
    }
    
    
    /// 区间
    func setcionNum()-> Int {
        return mDatasource.count
    }
    /// cell数量
    func cellCount(_ section: Int)-> Int {
        guard let items = mDatasource[section].items else { return 0 }
        return items.count
    }
    /// section 数据
    func fetchSectionData(_ section: Int)-> IPHomeSectionData {
        return mDatasource[section]
    }
    /// cell 数据
    func fetchCellData(_ index: IndexPath)->IPHomeItem? {
        guard let items = mDatasource[index.section].items else { return nil }
        return items[index.row]
    }
    
    /// 配置第一块区域数据
    private func configureBannerData(_ width: CGFloat)->IPHomeSectionData {
        /// 计算页头大小
        let sp: CGFloat = 10
        let finalWidth = width - 2 * sp
        
        let bannerHeight = 123.0 / 355.0 * finalWidth
        
        let funcHeight = 200.0 / 355.0 * finalWidth
        
        let finalHeight = bannerHeight + 20 + funcHeight + 20
        
        let size = CGSize.init(width: width, height: finalHeight)
        
        let f = IPHomeSectionData(headerReuseId: IPHomeHeaderReusableView.reuseId, headerSize: size, cellReuseId: nil, itemSize: .zero, items: nil)
        return f
    }
    
    /// 配置第二块区域数据
    private func configureItemData(_ width: CGFloat)->IPHomeSectionData{
        let sp: CGFloat = 3
        let size = CGSize.init(width: width - 2 * sp, height: 80)
        
        let headerSize = CGSize.init(width: width, height: 56)
        
        let items = [IPHomeItem(),IPHomeItem(),IPHomeItem(),IPHomeItem(),IPHomeItem(),IPHomeItem()]
        let f = IPHomeSectionData(headerReuseId: IPHomeTitleReusableView.reuseId, headerSize: headerSize, cellReuseId: IPHomeItemCell.reuseId, itemSize: size, items: items)
        return f
    }
}
